package com.wenda.homeschooedu.daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.wenda.homeschooedu.DB.DB.Table;
import com.wenda.homeschooedu.DB.JDBCConnection;
import com.wenda.homeschooedu.dao.Iinform;
import com.wenda.homeschooedu.model.Information;

public class InformService implements Iinform {
	JDBCConnection jdbcConnection;
	Statement statement;

	public InformService() {
		jdbcConnection = new JDBCConnection();
		statement = jdbcConnection.getStm();
	}

	public List<Information> getInformationsByClassId(int class_id) {
		String sql = String.format(Table.Inform.SQL.SELECT_BY_CLASS_ID,
				class_id);
		List<Information> informations = new ArrayList<Information>();
		try {

			ResultSet resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				Information information = new Information();
				information
						.set_id(resultSet.getInt(resultSet.findColumn("_id")));
				information.setAuthor_name(resultSet.getString(resultSet
						.findColumn("teacher_name")));
				information.setAuthor_id(resultSet.getInt(resultSet
						.findColumn("teacher_id")));
				information.setClass_id(resultSet.getInt(resultSet
						.findColumn("class_id")));
				information.setContent(resultSet.getString(resultSet
						.findColumn("thecontent")));
				information.setEvaluation(1);
				information.setTime(resultSet.getString(resultSet
						.findColumn("thetime")));
				information.setTitle(resultSet.getString(resultSet
						.findColumn("title")));
				informations.add(information);

			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return informations;
	}

	public List<Information> getInformationsByStudentId(int student_id) {
		String sql = String.format(
				Table.Student.SQL.SELECT_CLASSID_BY_STUDENTID, student_id);
		List<Information> informations = new ArrayList<Information>();
		int class_id = 0;

		try {
			ResultSet resultSet = statement.executeQuery(sql);
			class_id = resultSet.getInt(resultSet.findColumn("class_id"));
			informations = this.getInformationsByClassId(class_id);

		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}

		return informations;
	}

	public List<Information> getNewInformationsByStudentId(int student_id,
			int oldInform_Id) {
		String sql = String.format(
				Table.Student.SQL.SELECT_CLASSID_BY_STUDENTID, student_id);
		List<Information> informations = new ArrayList<Information>();
		int class_id = 0;

		try {
			ResultSet resultSet = statement.executeQuery(sql);
			resultSet.next();
			class_id = resultSet.getInt(resultSet.findColumn("class_id"));
			informations = this.getNewInformations(class_id, oldInform_Id);

		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}

		return informations;
	}

	public List<Information> getNewInformations(int class_id, int oldinform_id) {
		String sql = String.format(Table.Inform.SQL.SELECT_BY_OLD_ID,
				oldinform_id, class_id);
		List<Information> informations = new ArrayList<Information>();

		try {
			ResultSet resultSet = statement.executeQuery(sql);

			while (resultSet.next()) {
				Information information = new Information();
				information
						.set_id(resultSet.getInt(resultSet.findColumn("_id")));
				information.setAuthor_name(resultSet.getString(resultSet
						.findColumn("teacher_name")));
				information.setAuthor_id(resultSet.getInt(resultSet
						.findColumn("teacher_id")));
				information.setClass_id(resultSet.getInt(resultSet
						.findColumn("class_id")));
				information.setContent(resultSet.getString(resultSet
						.findColumn("thecontent")));
				information.setEvaluation(1);
				information.setTime(resultSet.getString(resultSet
						.findColumn("thetime")));
				information.setTitle(resultSet.getString(resultSet
						.findColumn("title")));
				informations.add(information);

			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

		return informations;
	}

}
